// pages/myorderbox/vieworderdetail/vieworderdetail.js
import {
  getOrderInfoForShare,
  getToken
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: "",
    dataInfo: null,
    phone: "",
    isShow: false,
    isShowCheckPhone: false,
    is_bussiness: false,
    isFocus: true,
    gb: false
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 登录 
    wx.login({
      success: lr => { 
        getToken(lr.code,(tr)=>{
          if(tr.code == 0){
                // options.orderId = 3774 //测试数据
            getOrderInfoForShare(tr.data.token, options.orderId, this.data.phone,"", (r) => {
              if (r.code == 0) {
                r.data.express_info.data.map((o, i) => {
                  o.m = o.time.substr(5, 5);
                  o.t = o.time.substr(11, 5);
                })
                this.setData({
                  isShow: true,
                  isShowCheckPhone: false,
                  dataInfo: r.data,
                  is_bussiness: r.is_bussiness,
                  orderId: options.orderId,
                  token: tr.data.token
                })

              } else if (r.code == 1) {
                this.setData({
                  isShow: true,
                  isShowCheckPhone: true,
                  dataInfo: r.data,
                  orderId: options.orderId,
                  token: tr.data.token
                })
              } else { 
                errorMsg(r.message);
              }
            })

          }else{ 
            errorMsg(r.message);
          } 
        }) 
      }
    })

    setInterval((r) => {

      this.setData({
        gb: !this.data.gb
      })

    }, 700)  
  },
  getPhone: function(e) {
    if (e.detail.value.length > 4) {
      this.setData({
        phone: e.detail.value.substr(0, 4)
      })
    } else {
      this.setData({
        phone: e.detail.value
      })
    }

  },
  //验证手机号
  onCheck: function(e) {
    let {
      orderId,
      phone,
      token
    } = this.data;
    getOrderInfoForShare(token,orderId, phone,e.detail.formId, (r) => {
      if (r.code == 0) {
        r.data.express_info.data.map((o, i) => {
          o.m = o.time.substr(5, 5);
          o.t = o.time.substr(11, 5);
        })
        this.setData({
          isShow: true,
          isShowCheckPhone: false,
          dataInfo: r.data,
          is_bussiness: r.is_bussiness,
        })

      } else if (r.code == 1) {
        this.setData({
          isShow: true,
          isShowCheckPhone: true,
          dataInfo: r.data,
        }) 
        errorMsg(r.message);
      } else { 
        errorMsg(r.message);
      }
    })
  },
  //图片预览
  previewImage: function(e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current[0], // 当前显示图片的http链接		  	
      urls: current // 需要预览的图片http链接列表	
    })
  },
  //复制
  onCopy: function(e) {
    let {
      ordernum
    } = e.currentTarget.dataset;
    wx.setClipboardData({
      data: ordernum,
      success() {
        errorMsg("复制成功")
      }
    })
  },
  //回首页
  goHome: function() {
    wx.reLaunch({
      url: '../../about/about',
    })
  },
  //点击输入框
  onInput: function() {
    this.setData({
      isFocus: true
    })
  },

})